﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess_Layer;
using Business_Layer;
using System.Collections;
public partial class MyAdmin_NhaCungCap_AM : System.Web.UI.Page
{
    LogAction log = new LogAction();
    protected void Page_Load(object sender, EventArgs e)
    {
        //Phân quyền
        if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenXem)
        {
            // xử lý quyền xem
            ((CommandField)gvDSNhaCungCap_AM.Columns[0]).ShowDeleteButton = false;
            CommandField dtCmdField = ((CommandField)dtNhaCungCap_AM.Fields[dtNhaCungCap_AM.Fields.Count - 1]);
            dtCmdField.ShowEditButton = false;
            dtCmdField.ShowInsertButton = false;
            XoaNhieuBUTTON.Visible = false;
        }
        else if (Helper.getCurrentAdminQuyen(this.Master) == QuyenTruyCap.QuyenChinhSua)
        {
            // xử lý quyền chỉnh sửa
        }

        lblThongBaoDTView.Visible = false;
        lblThongBaoGridView.Visible = false;
    }
    private void refreshDetailViewPageIndex()
    {
        dtNhaCungCap_AM.DataBind();
        dtNhaCungCap_AM.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaCungCap_AM);
    }
    protected void gvDSNhaCungCap_AM_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Helper.changeGVPageIndex(gvDSNhaCungCap_AM, e.NewPageIndex);
        gvDSNhaCungCap_AM.SelectedIndex = gvDSNhaCungCap_AM.Rows.Count > 0 ? 0 : -1;
        gvDSNhaCungCap_AM.PageIndex = Helper.calPageIndexForDetailView(gvDSNhaCungCap_AM);
    }
    protected void gvDSNhaCungCap_AM_PageIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }
    protected void gvDSNhaCungCap_AM_SelectedIndexChanged(object sender, EventArgs e)
    {
        refreshDetailViewPageIndex();
    }
    protected void dtNhaCungCap_AM_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        gvDSNhaCungCap_AM.DataBind();
        Helper.changeGVPageIndex(gvDSNhaCungCap_AM, gvDSNhaCungCap_AM.PageCount - 1);
        gvDSNhaCungCap_AM.SelectedIndex = gvDSNhaCungCap_AM.Rows.Count - 1;
        refreshDetailViewPageIndex();
    }
    protected void dtNhaCungCap_AM_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        lblThongBaoDTView.Visible = true;
        string id = e.Keys[0].ToString();

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Cập nhật thành công record có ID là " + id;
            return;
        }

        lblThongBaoDTView.Text = "Cập nhật thất bại record!";
        e.ExceptionHandled = true;

        gvDSNhaCungCap_AM.DataBind();      
        refreshDetailViewPageIndex();  
    }
    protected void gvDSNhaCungCap_AM_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        if (e.Exception != null)
        {
            string undeletedID = e.Keys[0].ToString();
            Response.Write(Helper.CreateMessageBox("Không thể xóa Record có ID " + undeletedID));
            e.ExceptionHandled = true;
        }
        else
        {
            long id = long.Parse(e.Keys[0].ToString());
            Response.Write(Helper.CreateMessageBox("Đã xóa Record có ID = " + id));
            dtNhaCungCap_AM.DataBind();            
        }
    }
    protected void XoaNhieuBUTTON_Click(object sender, EventArgs e)
    {
        string ids = "";
        List<long> deletedIDs = new List<long>();
        int lastCellIndex = gvDSNhaCungCap_AM.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvDSNhaCungCap_AM.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                long id = long.Parse(r.Cells[1].Text);
                deletedIDs.Add(id);
                ids += id.ToString() + ", ";
            }
        }

        lblThongBaoGridView.Visible = true;
        lblThongBaoGridView.Text = "Xóa thành công record: " + ids.Substring(0, ids.LastIndexOf(", "));
        //log
        log.TenLoaiCapNhat = "Xóa";
        log.Ngay = DateTime.Now;
        log.TenBang = "Bảng Nhà cung cấp";
        log.TenLoaiDangNhap = "Admin";
        log.TenDangNhap = Helper.getCurrentAdminUserName(Session);
        string logText = "";
        log.DuLieuCu = "record ID:" + ids.Substring(0, ids.LastIndexOf(", "));
        logText = "X";
        log.DuLieuMoi = logText;
        Log_BUS.insert(log);
        //log
        try
        {
            DataAccess_Layer.NhaCungCapDAC.delete(deletedIDs);
        }
        catch (System.Exception ex)
        {
            lblThongBaoGridView.Text = "Xóa thất bại!";
        }
          
        gvDSNhaCungCap_AM.DataBind();
        dtNhaCungCap_AM.DataBind();
    }
    protected void LQDSHoaDon_AM_Inserted(object sender, LinqDataSourceStatusEventArgs e)
    {
        //log
        try
        {
            log.TenLoaiCapNhat = "Thêm";
            log.Ngay = DateTime.Now;
            log.TenBang = "Bảng Nhà cung cấp";
            log.TenLoaiDangNhap = "Adminstator";
            log.TenDangNhap = Helper.getCurrentAdminUserName(Session);

            DataAccess_Layer.NhaCungCap logO;

            log.DuLieuCu = "X";

            logO = (DataAccess_Layer.NhaCungCap)e.Result;
            log.DuLieuMoi = "Tên đăng nhập: " + logO.TenDangNhap
                + "..\nMật khẩu: " + logO.MatKhau
                + "..\nWS Url: " + logO.WSUrl
                + "..\nTên: " + logO.Ten
                + "..\nĐịa chỉ: " + logO.DiaChi
                + "..\nSố điện thoại: " + logO.SoDT
                + "..\nTên Tài khoản NKTH 01: " + logO.TenTKNganHang01
                  + "..\nSố Tài khoản NKTH 01: " + logO.SoTKNganHang01
                   + "..\nTên Tài khoản NKTH 02: " + logO.TenTKNganHang02
                  + "..\nSố Tài khoản NKTH 02: " + logO.SoTKNganHang02
                      + "..\nQuốc gia: " + logO.QuocGia
                + "..\nĐã xóa: " + logO.DaXoa;
            Log_BUS.insert(log);
        }
        catch { }
        //log

        lblThongBaoDTView.Visible = true;
        DataAccess_Layer.NhaCungCap newOrder = (DataAccess_Layer.NhaCungCap)e.Result;

        if (e.Exception == null)
        {
            lblThongBaoDTView.Text = "Thêm thành công record có ID là " + newOrder.ID;
            return;
        }

        lblThongBaoDTView.Text = "Thêm thất bại record!";
        e.ExceptionHandled = true;
    }
    protected void LQDSNhaCungCap_AM_Updating(object sender, LinqDataSourceUpdateEventArgs e)
    {

    }
}